package com.yuntsg.nnsfcp.test;

import com.yuntsg.nnsfcp.service.front.pojo.CnkiListEntity;
import com.yuntsg.nnsfcp.util.AuthCode;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

//2020年9月30日13:20:04 知网详情测试
public class CnkiNew {
    public static void main(String[] args) {
//        String oldhtml = FileUtil.readUtf8String("C:\\Users\\GCR\\Desktop\\cnkiold.html");
//        String newhtml = FileUtil.readUtf8String("C:\\Users\\GCR\\Desktop\\cnkinew.html");
////        CnkiListEntity cnkiListEntity = parsingHtml(oldhtml);
//        CnkiListEntity cnkiListEntityNew = parsingNewHtml(newhtml);
////        System.out.println(cnkiListEntity);
//        System.out.println(cnkiListEntityNew);

        String PASS = "123456";
        String s = AuthCode.encodePass(PASS);
        System.out.println(s);
    }


    private static CnkiListEntity parsingNewHtml(String html) {
        Document document = Jsoup.parse(html);
        //封装解析完成的详情页上的结果
        CnkiListEntity cnkiListEntity = new CnkiListEntity();
        // 标题
        Element titleEleDiv = document.select("div.wx-tit").first();
        if (titleEleDiv != null) {
            String h1 = "";
            if (titleEleDiv.select("h1").first() != null) {
                h1 = titleEleDiv.selectFirst("h1").text();
            }
            cnkiListEntity.setTitle(h1);

            // 机构
            int organizationEle = titleEleDiv.select("h3").size();
            if (organizationEle > 1) {
                Element aEles = titleEleDiv.select("h3").get(1);
                cnkiListEntity.setOrganization(aEles.text());
            }


        }


        // 作者
        Element authorEle = document.select("h3#authorpart").first();
        if (authorEle != null) {
            String author = "";
            String text = authorEle.text();
            if (StringUtils.isNotEmpty(text)) {
                author = text;
            }
            cnkiListEntity.setAuthor(author);
        }


        // 摘要正文
        Element contentEle = document.select("#ChDivSummary").first();
        if (contentEle != null) {
            String content = contentEle.text();
            //System.out.println(content);
            cnkiListEntity.setContent(content);
        }

        // 关键字
        Element keyWordEle = document.select("p.keywords").first();
        if (keyWordEle != null) {
            cnkiListEntity.setKeyword(keyWordEle.select("a").text());
        }

        // doi号码
        Element doiEle = document.select("li:contains(DOI：)").first();
        if (doiEle != null) {
            Element parent = doiEle.select("p").first();
            if (parent != null) {
                cnkiListEntity.setDoi(parent.text());
            }
        }

        // 分类号
        Element categoryNumEle = document.select("li:contains(分类号：)").first();
        if (categoryNumEle != null) {
            Element parent = categoryNumEle.select("p").first();
            if (parent != null) {
                cnkiListEntity.setCategoryNum(parent.text());
            }
        }


        // CAJ下载链接, PDF下载链接  TODO 这里的链接获取到要登陆,不封装了
        Element downLinkEle = document.select("#DownLoadParts").first();
        if (downLinkEle != null) {
            // caj下载链接
            Element cajEle = downLinkEle.select("a:contains(CAJ)").first();
            if (cajEle != null) {
                String cajHref = cajEle.attr("href");
                cajHref = cajHref.replace(" ", "").replace("\n", "");
                //System.out.println(cajHref);
            }

            // pdf下载链接
            Element pdfEle = downLinkEle.select("a:contains(PDF)").first();
            if (pdfEle != null) {
                String pdfHref = pdfEle.attr("href");
                pdfHref = pdfHref.replace(" ", "").replace("\n", "");
                //System.out.println(pdfHref);
            }
        }

        // 下载|页码|总数|大小
        Element totalEle = document.select("p.total-inform").first();
        if (totalEle != null) {
            // 下载
            Element downEle = totalEle.select("span:contains(下载：)").first();
            if (downEle != null) {
                String down = downEle.text();
                down = down.replace("下载：", "");
                //System.out.println(down);
                cnkiListEntity.setDownNum(down);
            }

            // 页码
            Element pageEle = totalEle.select("span:contains(页码：)").first();
            if (pageEle != null) {
                String page = pageEle.text();
                page = page.replace("页码：", "");
                //System.out.println(page);
                cnkiListEntity.setPage(page);
            }

            // 页数
            Element pageNumEle = totalEle.select("span:contains(页数：)").first();
            if (pageNumEle != null) {
                String pageNum = pageNumEle.text();
                pageNum = pageNum.replace("页数：", "");
                //System.out.println(pageNum);
                cnkiListEntity.setPageNum(pageNum);
            }

            // 大小
            Element sizeEle = totalEle.select("span:contains(大小：)").first();
            if (sizeEle != null) {
                String size = sizeEle.text();
                size = size.replace("大小：", "");
                //System.out.println(size);
                cnkiListEntity.setSize(size);
            }
        }


        // 文章|来源 日期 页码
        Element sourceEle = document.select("div.top-tip").first();
        if (sourceEle != null) {

            // 来源名
            Element nameCnEle = sourceEle.select("span").first();
            if (nameCnEle != null) {
                if (nameCnEle.select("a").size() > 0) {
                    // 来源中文名
                    String nameCn = nameCnEle.select("a").first().text();
                    cnkiListEntity.setSourceCn(nameCn);
                }

                // 来源英文名 没有
                cnkiListEntity.setSourceEn("");

                // 年|期
                if (nameCnEle != null) {
                    if (nameCnEle.select("a").size() > 1) {
                        // 来源中文名
                        String nameCn = nameCnEle.select("a").get(1).text();
                        cnkiListEntity.setNianQi(nameCn);
                    }
                }

                // 什么核心期刊 ??
                if (nameCnEle != null) {
                    if (nameCnEle.select("a").size() > 1) {
                        // 来源中文名
                        Element coreJournal = document.select("a.type").first();
                        if (coreJournal != null) {
                            cnkiListEntity.setCoreJournal(coreJournal.text());
                        }
                    }
                }

            }


            // issn号
            cnkiListEntity.setIssn("");

        }
        return cnkiListEntity;
    }

}
